import java.util.Scanner;

public class Queue {
    public static void main(String[] args) {
        methodQueue q = new methodQueue(3);
        System.out.println("----------------------------------");
        System.out.println("1:增加用户");
        System.out.println("2:查看全部用户");
        System.out.println("3:抽取用户");
        System.out.println("4:接受程序");
        System.out.println("----------------------------------");
        boolean isFlag = true;
        Scanner scanner = new Scanner(System.in);
        char a =' ';
        while (isFlag){
                    a=scanner.next().charAt(0);
                switch (a){
                    case '1':
                        System.out.print("请输出一个数:");
                        q.addQueue(scanner.nextInt());
                        System.out.println("----------------");
                        break;
                    case '2':
                        System.out.println("----------------");
                        q.showArr();
                        System.out.println("----------------");
                        break;
                    case '3':
                        q.getQueue();
                        break;
                    case '4':
                        isFlag=false;
                        System.out.println("程序结束！！");
                        break;
                }
        }

    }
}
class methodQueue{
     private int maxSize;
     private int read;
     private int font;
     private int [] arr;

   public methodQueue(){

    }
    public methodQueue(int maxSize) {
        this.maxSize = maxSize;
        this.arr = new int[maxSize];
    }
    public boolean isFull(){
        //保证read在0-2取 环形
        return (read + 1) % maxSize == font;
    }
    public  boolean isEmpty(){
       return read==font;
    }
    public void addQueue(int n){
       if(isFull()){
           System.out.println("队列已满");
           return;
       }
        arr[read]=n;
       //保证read在0-2取 环形
        read = (read+1) % maxSize;
    }
    //抽取数据
    public int getQueue(){
       if(isEmpty()){
           throw new RuntimeException("队列是空的");
       }
        int i = arr[font];
       //保证font 在0-2循环取 环形
        font= (font+1) % maxSize;
        return i;
    }
    //展现所有元素
    public void showArr(){
       if(isEmpty()){
           System.out.println("队列为空");
           return;
       }
       //当font为2  有效数字为2时  若i<(read+maxSize-font)%maxSize  不加2 就会不进入for循环 因此要加font
       for (int i = font ; i < font + (read+maxSize-font)%maxSize;i++){
           //防止越界 余数永远在其 [0,maxsize-1]
           System.out.println(arr[i%maxSize]);
       }
    }
    //取头元素
    public int headQueue(){
       if(isEmpty()){
           throw new RuntimeException("队列为空");
       }
       //+1因为front是-1前一个的
           return arr[font];
    }
}
